package com.zgkxzx.android.providertest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

import com.zgkxzx.android.greendao.StudentOpsDao;

/**
 * 创建者 zgkxzx
 * 日期   1/29/18
 * 描述
 */

public class StudentContentProvider extends ContentProvider {
    private final static String TAG = StudentContentProvider.class.getSimpleName();
    public static final int BOOK_URI_CODE = 0;

    public static final int USER_URI_CODE = 1;

    private UriMatcher uriMatcher;

    private StudentOpsDao studentOpsDao;

    private DBHelper dbHelper;

    private SQLiteDatabase sqLiteDatabase;

    private Context context;


    @Override
    public boolean onCreate() {
        Log.d(TAG, "....onCreate....");
        context = getContext();
        //studentOpsDao = StudentOpsDao.getInstance();
        dbHelper = new DBHelper(context);
        initProviderData();
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI("com.zgkxzx.android.providertest.StudentContentProvider", DBHelper.BOOK_TABLE_NAME, BOOK_URI_CODE);
        uriMatcher.addURI("com.zgkxzx.android.providertest.StudentContentProvider", DBHelper.USER_TABLE_NAME, USER_URI_CODE);// #匹配所有数字，*匹配所有字符
        return false;
    }


    //初始化原始数据
    private void initProviderData() {
        sqLiteDatabase = new DBHelper(context).getWritableDatabase();
        sqLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("bookName", "数据结构");
        sqLiteDatabase.insert(DBHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.put("bookName", "编译原理");
        sqLiteDatabase.insert(DBHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.put("bookName", "网络原理");
        sqLiteDatabase.insert(DBHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.clear();

        contentValues.put("userName", "叶");
        contentValues.put("sex", "女");
        sqLiteDatabase.insert(DBHelper.USER_TABLE_NAME, null, contentValues);
        contentValues.put("userName", "叶叶");
        contentValues.put("sex", "男");
        sqLiteDatabase.insert(DBHelper.USER_TABLE_NAME, null, contentValues);
        contentValues.put("userName", "叶应是叶");
        contentValues.put("sex", "男");
        sqLiteDatabase.insert(DBHelper.USER_TABLE_NAME, null, contentValues);
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }


    /*
     public Cursor query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy) {
    */
    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "....query....");
        String tableName = getTableName(uri);
        return sqLiteDatabase.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null);
    }


    private String getTableName(Uri uri) {
        String tableName = null;
        switch (uriMatcher.match(uri)) {
            case BOOK_URI_CODE:
                tableName = DBHelper.BOOK_TABLE_NAME;
                break;
            case USER_URI_CODE:
                tableName = DBHelper.USER_TABLE_NAME;
                break;
        }
        return tableName;
    }


    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.d(TAG, "....getType....");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d(TAG, "....insert....");
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unsupported URI:" + uri);
        }
        sqLiteDatabase.insert(tableName, null, values);
        context.getContentResolver().notifyChange(uri, null);
        return uri;

    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.d(TAG, "....delete....");
        String tableName = getTableName(uri);
        sqLiteDatabase.delete(tableName, selection, selectionArgs);
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        Log.d(TAG, "....update....");
        String tableName = getTableName(uri);
        sqLiteDatabase.update(tableName, values, selection, selectionArgs);
        return 0;
    }
}
